package com.alumni_circle.controller.home.addition;

import cn.hutool.core.util.ObjectUtil;
import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.utils.SecurityUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.home.addition.LikeCommentsSum;
import com.alumni_circle.service.home.addition.impl.LikeCommentsSumServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 龙小虬
 * @since 2020-08-30 22:35
 */
@RestController
public class LikeCommentsSumController extends BaseController<LikeCommentsSum> {


    @Resource
    LikeCommentsSumServiceImpl likeCommentsSumService;

    /**
     * 查询是否点赞文章
     *
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getLikeComments", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询是否点赞评论", notes = "查询是否点赞评论", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询是否点赞评论",url="/getLikeComments")
    public ResponseModel getLikeComments(@ApiParam(required = true, name = "baseId")
                                        @RequestBody BaseId baseId) {
        QueryWrapper<LikeCommentsSum> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", SecurityUtils.getId());
        queryWrapper.eq("comments_id",baseId.getId());
        if(ObjectUtil.hasEmpty(likeCommentsSumService.getOne(queryWrapper))){
            return new ResponseModel<>(false);
        }
        return new ResponseModel<>(true);
    }

    /**
     * 增加文章点赞
     *
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateLikeComments", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新评论点赞", notes = "更新评论点赞", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新评论点赞",url="/updateLikeComments")
    public ResponseModel updateLikeComments(@ApiParam(required = true, name = "baseId")
                                    @RequestBody BaseId baseId) {
        QueryWrapper<LikeCommentsSum> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",SecurityUtils.getId());
        queryWrapper.eq("comments_id",baseId.getId());
        LikeCommentsSum likeComments = likeCommentsSumService.getOne(queryWrapper);
        if(ObjectUtil.hasEmpty(likeComments)){
            LikeCommentsSum likeCommentsSum = new LikeCommentsSum();
            likeCommentsSum.setCommentsId(baseId.getId());
            likeCommentsSum.setId(SecurityUtils.getId());
            likeCommentsSumService.save(likeCommentsSum);
            // 增加
            return new ResponseModel<>(true);
        }
        // 不增加
        likeCommentsSumService.removeById(likeComments.getId());
        return new ResponseModel<>(false);
    }


}
